<?php


namespace App\Http\Controllers\Drivers\V2\Drivers;


use App\Http\Controllers\Controller;
use App\Http\Requests\UsersRequests;
use App\Services\UsersService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    /**
     * @return \Illuminate\Http\JsonResponse
     * 用户信息
     */
    public function read(UsersService $service)
    {
        return $this->sendJson($service->user_info(Auth::user()));
    }

    /**
     * 设置密码
     */
    public function modification(Request $request, UsersService $service, UsersRequests $userValidate)
    {
        $request_data = $request->post();
        if (!$userValidate->scene('modification')->check($request_data)) {
            return $this->sendError($userValidate->getError());
        }
        $requests = $service->password_confirmed($request_data);
        if ($requests) {
            return $this->sendJson('设置成功');
        } else {
            return $this->sendError('设置失败');
        }
    }

    /**
     * 钱包
     */
    public function wallet(Request $request, UsersService $service)
    {
        $type = (string)$request->post('type', '0');
        return $this->sendJson($service->wallet_count($type));
    }

    /**
     * 账户明细
     */
    public function wallet_list(Request $request, UsersService $service)
    {
        $date = $request->post('date');
        $type = $request->post('type');
        $page = $request->post('page', $this->page);
        $size = $request->post('size', $this->size);
        $startOfMonth = Carbon::now()->startOfMonth();
        $endOfMonth = Carbon::now()->endOfMonth();
        if (!empty($date)) {
            $dates = $this->get_monthinfo_by_date($date);
            $startOfMonth = $dates['month_start_day'];
            $endOfMonth = $dates['month_end_day'];
        }
        return $this->sendJson($service->wallet_list(Auth::user(), $startOfMonth, $endOfMonth, $type, $page, $size));
    }

    /**
     * 根据指定日期获取所在月的起始时间和结束时间
     */
    protected function get_monthinfo_by_date($date)
    {
        $ret = array();
        $timestamp = strtotime($date);
        $mdays = date('t', $timestamp);
        return array(
            'month_start_day' => date('Y-m-1', $timestamp),
            'month_end_day' => date('Y-m-' . $mdays, $timestamp)
        );
    }

    /**
     * 提现/结算详情
     */
    public function driver_wallet_details(Request $request, UsersService $service)
    {
        $id = (int)$request->post('id');
        if (empty($id)) $this->sendError('ID不能为空');
        $http_response_header = $service->driver_wallet_details($id);
        return $this->sendJson($http_response_header);
    }

    /**
     * 修改密码
     */
    public function password_edits(Request $request, UsersService $usersService, UsersRequests $userValidate)
    {
        $request_data = $request->post();
        if (!$userValidate->scene('modification')->check($request_data)) {
            return $this->sendError($userValidate->getError());
        }
        $reselut = $usersService->ChangePassword($request_data);
        return $this->sendSuccess('修改成功');
    }

    /**
     * 密码检测
     */
    public function password_check(Request $request, UsersService $usersService)
    {
        $res = $usersService->password_check(Auth::user());
        return $this->sendJson($res);
    }

    /**
     * 忘记密码
     */
    public function Forget_password(Request $request, UsersService $usersService)
    {
        $phone = $request->post('phone');
        if (empty($phone)) return_bobc('手机号必须');
        $code = $request->post('code');
        if (empty($code)) return_bobc('验证码必须');
        $password = $request->post('password');
        if (empty($password)) return_bobc('新密码必须');
        $reselut = $usersService->Forget_password($phone, $code, $password);
        if ($reselut == true) {
            return $this->sendSuccess('修改成功');
        }
        return $this->sendError('修改失败');
    }
}